<?php
    require('config.php');

    //Conectamos con la base de datos
    $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('MySQL error: ' . mysql_error());
    mysql_select_db(DB_NAME) or die('MySQL error: Unable to select database');

    $today = date('Y-m-d');

    //Online
    $result = mysql_query("SELECT COUNT(*) FROM stats WHERE date='$today'");
    $row = mysql_fetch_array($result);
    $online = $row['COUNT(*)'];
    //Totales
    $result = mysql_query('SELECT COUNT(DISTINCT user_id) FROM stats');
    $row = mysql_fetch_array($result);
    $total = $row['COUNT(DISTINCT user_id)'];
    //Chart
    $result = mysql_query('SELECT COUNT(*) FROM stats GROUP BY date');
    
    $sresult = array();
    $max = 0;
    while($row = mysql_fetch_array($result)){
        $value = $row['COUNT(*)'];

        if ($max < $value) $max = $value;

        array_push($sresult, $value);
    }

    for($i=0;$i<count($sresult);$i++)  {
        $sresult[$i] = round(($sresult[$i] * 100) / $max);
    }

    $chart = implode(',', $sresult);
    
    //Descargas
    //$result = mysql_query('SELECT SUM(counter) FROM stats');
    //Descargas Hoy
    //$result = mysql_query("SELECT SUM(counter) FROM stats WHERE date='$today'");
    //Descargas por dia
    //$result = mysql_query('SELECT SUM(counter) FROM stats GROUP BY date');
    
    echo "<html><head><script>window.parent.postMessage('{\"type\":\"stats\",\"chart\":[$chart],\"online\":$online,\"total\":$total}', 'http://www.tuenti.com');</script></head></html>"
?>